package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcClbxdMapper;
import com.ruoyi.gcgl.domain.GcClbxd;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.service.IGcClbxdService;
import com.ruoyi.common.core.text.Convert;

/**
 * 差旅报销单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Service
public class GcClbxdServiceImpl implements IGcClbxdService 
{
    @Autowired
    private GcClbxdMapper gcClbxdMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询差旅报销单
     * 
     * @param id 差旅报销单主键
     * @return 差旅报销单
     */
    @Override
    public GcClbxd selectGcClbxdById(Long id)
    {
        return gcClbxdMapper.selectGcClbxdById(id);
    }

    /**
     * 查询差旅报销单列表
     * 
     * @param gcClbxd 差旅报销单
     * @return 差旅报销单
     */
    @Override
    public List<GcClbxd> selectGcClbxdList(GcClbxd gcClbxd)
    {
        return gcClbxdMapper.selectGcClbxdList(gcClbxd);
    }

    /**
     * 新增差旅报销单
     * 
     * @param gcClbxd 差旅报销单
     * @return 结果
     */
    @Override
    public int insertGcClbxd(GcClbxd gcClbxd)
    {
        int rows = gcClbxdMapper.insertGcClbxd(gcClbxd);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcClbxd.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcClbxd.getOwner());
        variables.put("level1", gcClbxd.getZmr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjcailvbaoxiao", String.valueOf(gcClbxd.getId()), variables);
    	
        GcClbxd obj = new GcClbxd();
    	obj.setId(gcClbxd.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcClbxdMapper.updateGcClbxd(obj);
    	
        return rows;
    }

    /**
     * 修改差旅报销单
     * 
     * @param gcClbxd 差旅报销单
     * @return 结果
     */
    @Override
    public int updateGcClbxd(GcClbxd gcClbxd)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcClbxd.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcClbxd.getOwner());
//        variables.put("level1", gcClbxd.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjcailvbaoxiao", String.valueOf(gcClbxd.getId()), variables);
//        gcClbxd.setProcessInstanceId(pi.getProcessInstanceId());
        return gcClbxdMapper.updateGcClbxd(gcClbxd);
    }

    /**
     * 批量删除差旅报销单
     * 
     * @param ids 需要删除的差旅报销单主键
     * @return 结果
     */
    @Override
    public int deleteGcClbxdByIds(String ids)
    {
        return gcClbxdMapper.deleteGcClbxdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除差旅报销单信息
     * 
     * @param id 差旅报销单主键
     * @return 结果
     */
    @Override
    public int deleteGcClbxdById(Long id)
    {
        return gcClbxdMapper.deleteGcClbxdById(id);
    }

	@Override
	public int updateGcClbxd4Check(GcClbxd gcClbxd) {
		return gcClbxdMapper.updateGcClbxd(gcClbxd);
	}
}
